home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / re.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  13KB  |  331 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Support for regular expressions (RE).
  5.  
  6. This module provides regular expression matching operations similar to
  7. those found in Perl.  It supports both 8-bit and Unicode strings; both
  8. the pattern and the strings being processed can contain null bytes and
  9. characters outside the US ASCII range.
  10.  
  11. Regular expressions can contain both special and ordinary characters.
  12. Most ordinary characters, like "A", "a", or "0", are the simplest
  13. regular expressions; they simply match themselves.  You can
  14. concatenate ordinary characters, so last matches the string \'last\'.
  15.  
  16. The special characters are:
  17.     "."      Matches any character except a newline.
  18.     "^"      Matches the start of the string.
  19.     "$"      Matches the end of the string or just before the newline at
  20.              the end of the string.
  21.     "*"      Matches 0 or more (greedy) repetitions of the preceding RE.
  22.              Greedy means that it will match as many repetitions as possible.
  23.     "+"      Matches 1 or more (greedy) repetitions of the preceding RE.
  24.     "?"      Matches 0 or 1 (greedy) of the preceding RE.
  25.     *?,+?,?? Non-greedy versions of the previous three special characters.
  26.     {m,n}    Matches from m to n repetitions of the preceding RE.
  27.     {m,n}?   Non-greedy version of the above.
  28.     "\\\\"     Either escapes special characters or signals a special sequence.
  29.     []       Indicates a set of characters.
  30.              A "^" as the first character indicates a complementing set.
  31.     "|"      A|B, creates an RE that will match either A or B.
  32.     (...)    Matches the RE inside the parentheses.
  33.              The contents can be retrieved or matched later in the string.
  34.     (?iLmsux) Set the I, L, M, S, U, or X flag for the RE (see below).
  35.     (?:...)  Non-grouping version of regular parentheses.
  36.     (?P<name>...) The substring matched by the group is accessible by name.
  37.     (?P=name)     Matches the text matched earlier by the group named name.
  38.     (?#...)  A comment; ignored.
  39.     (?=...)  Matches if ... matches next, but doesn\'t consume the string.
  40.     (?!...)  Matches if ... doesn\'t match next.
  41.     (?<=...) Matches if preceded by ... (must be fixed length).
  42.     (?<!...) Matches if not preceded by ... (must be fixed length).
  43.     (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
  44.                        the (optional) no pattern otherwise.
  45.  
  46. The special sequences consist of "\\\\" and a character from the list
  47. below.  If the ordinary character is not on the list, then the
  48. resulting RE will match the second character.
  49.     \\number  Matches the contents of the group of the same number.
  50.     \\A       Matches only at the start of the string.
  51.     \\Z       Matches only at the end of the string.
  52.     \\b       Matches the empty string, but only at the start or end of a word.
  53.     \\B       Matches the empty string, but not at the start or end of a word.
  54.     \\d       Matches any decimal digit; equivalent to the set [0-9].
  55.     \\D       Matches any non-digit character; equivalent to the set [^0-9].
  56.     \\s       Matches any whitespace character; equivalent to [ \\t\\n\\r\\f\\v].
  57.     \\S       Matches any non-whitespace character; equiv. to [^ \\t\\n\\r\\f\\v].
  58.     \\w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_].
  59.              With LOCALE, it will match the set [0-9_] plus characters defined
  60.              as letters for the current locale.
  61.     \\W       Matches the complement of \\w.
  62.     \\\\       Matches a literal backslash.
  63.  
  64. This module exports the following functions:
  65.     match    Match a regular expression pattern to the beginning of a string.
  66.     search   Search a string for the presence of a pattern.
  67.     sub      Substitute occurrences of a pattern found in a string.
  68.     subn     Same as sub, but also return the number of substitutions made.
  69.     split    Split a string by the occurrences of a pattern.
  70.     findall  Find all occurrences of a pattern in a string.
  71.     finditer Return an iterator yielding a match object for each match.
  72.     compile  Compile a pattern into a RegexObject.
  73.     purge    Clear the regular expression cache.
  74.     escape   Backslash all non-alphanumerics in a string.
  75.  
  76. Some of the functions in this module takes flags as optional parameters:
  77.     I  IGNORECASE  Perform case-insensitive matching.
  78.     L  LOCALE      Make \\w, \\W, \\b, \\B, dependent on the current locale.
  79.     M  MULTILINE   "^" matches the beginning of lines (after a newline)
  80.                    as well as the string.
  81.                    "$" matches the end of lines (before a newline) as well
  82.                    as the end of the string.
  83.     S  DOTALL      "." matches any character at all, including the newline.
  84.     X  VERBOSE     Ignore whitespace and comments for nicer looking RE\'s.
  85.     U  UNICODE     Make \\w, \\W, \\b, \\B, dependent on the Unicode locale.
  86.  
  87. This module also defines an exception \'error\'.
  88.  
  89. '''
  90. import sys
  91. import sre_compile
  92. import sre_parse
  93. __all__ = [
  94.     'match',
  95.     'search',
  96.     'sub',
  97.     'subn',
  98.     'split',
  99.     'findall',
  100.     'compile',
  101.     'purge',
  102.     'template',
  103.     'escape',
  104.     'I',
  105.     'L',
  106.     'M',
  107.     'S',
  108.     'X',
  109.     'U',
  110.     'IGNORECASE',
  111.     'LOCALE',
  112.     'MULTILINE',
  113.     'DOTALL',
  114.     'VERBOSE',
  115.     'UNICODE',
  116.     'error']
  117. __version__ = '2.2.1'
  118. I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE
  119. L = LOCALE = sre_compile.SRE_FLAG_LOCALE
  120. U = UNICODE = sre_compile.SRE_FLAG_UNICODE
  121. M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE
  122. S = DOTALL = sre_compile.SRE_FLAG_DOTALL
  123. X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE
  124. T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE
  125. DEBUG = sre_compile.SRE_FLAG_DEBUG
  126. error = sre_compile.error
  127.  
  128. def match(pattern, string, flags = 0):
  129.     '''Try to apply the pattern at the start of the string, returning
  130.     a match object, or None if no match was found.'''
  131.     return _compile(pattern, flags).match(string)
  132.  
  133.  
  134. def search(pattern, string, flags = 0):
  135.     '''Scan through string looking for a match to the pattern, returning
  136.     a match object, or None if no match was found.'''
  137.     return _compile(pattern, flags).search(string)
  138.  
  139.  
  140. def sub(pattern, repl, string, count = 0, flags = 0):
  141.     """Return the string obtained by replacing the leftmost
  142.     non-overlapping occurrences of the pattern in string by the
  143.     replacement repl.  repl can be either a string or a callable;
  144.     if a string, backslash escapes in it are processed.  If it is
  145.     a callable, it's passed the match object and must return
  146.     a replacement string to be used."""
  147.     return _compile(pattern, flags).sub(repl, string, count)
  148.  
  149.  
  150. def subn(pattern, repl, string, count = 0, flags = 0):
  151.     """Return a 2-tuple containing (new_string, number).
  152.     new_string is the string obtained by replacing the leftmost
  153.     non-overlapping occurrences of the pattern in the source
  154.     string by the replacement repl.  number is the number of
  155.     substitutions that were made. repl can be either a string or a
  156.     callable; if a string, backslash escapes in it are processed.
  157.     If it is a callable, it's passed the match object and must
  158.     return a replacement string to be used."""
  159.     return _compile(pattern, flags).subn(repl, string, count)
  160.  
  161.  
  162. def split(pattern, string, maxsplit = 0, flags = 0):
  163.     '''Split the source string by the occurrences of the pattern,
  164.     returning a list containing the resulting substrings.'''
  165.     return _compile(pattern, flags).split(string, maxsplit)
  166.  
  167.  
  168. def findall(pattern, string, flags = 0):
  169.     '''Return a list of all non-overlapping matches in the string.
  170.  
  171.     If one or more groups are present in the pattern, return a
  172.     list of groups; this will be a list of tuples if the pattern
  173.     has more than one group.
  174.  
  175.     Empty matches are included in the result.'''
  176.     return _compile(pattern, flags).findall(string)
  177.  
  178. if sys.hexversion >= 33685504:
  179.     __all__.append('finditer')
  180.     
  181.     def finditer(pattern, string, flags = 0):
  182.         '''Return an iterator over all non-overlapping matches in the
  183.         string.  For each match, the iterator returns a match object.
  184.  
  185.         Empty matches are included in the result.'''
  186.         return _compile(pattern, flags).finditer(string)
  187.  
  188.  
  189. def compile(pattern, flags = 0):
  190.     '''Compile a regular expression pattern, returning a pattern object.'''
  191.     return _compile(pattern, flags)
  192.  
  193.  
  194. def purge():
  195.     '''Clear the regular expression cache'''
  196.     _cache.clear()
  197.     _cache_repl.clear()
  198.  
  199.  
  200. def template(pattern, flags = 0):
  201.     '''Compile a template pattern, returning a pattern object'''
  202.     return _compile(pattern, flags | T)
  203.  
  204. _alphanum = frozenset('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
  205.  
  206. def escape(pattern):
  207.     '''Escape all non-alphanumeric characters in pattern.'''
  208.     s = list(pattern)
  209.     alphanum = _alphanum
  210.     for i, c in enumerate(pattern):
  211.         if c not in alphanum or c == '\x00':
  212.             s[i] = '\\000'
  213.         else:
  214.             s[i] = '\\' + c
  215.     
  216.     return pattern[:0].join(s)
  217.  
  218. _cache = { }
  219. _cache_repl = { }
  220. _pattern_type = type(sre_compile.compile('', 0))
  221. _MAXCACHE = 100
  222.  
  223. def _compile(*key):
  224.     cachekey = (type(key[0]),) + key
  225.     p = _cache.get(cachekey)
  226.     if p is not None:
  227.         return p
  228.     (pattern, flags) = None
  229.     if isinstance(pattern, _pattern_type):
  230.         if flags:
  231.             raise ValueError('Cannot process flags argument with a compiled pattern')
  232.         return pattern
  233.     if not None.isstring(pattern):
  234.         raise TypeError, 'first argument must be string or compiled pattern'
  235.     
  236.     try:
  237.         p = sre_compile.compile(pattern, flags)
  238.     except error:
  239.         v = None
  240.         raise error, v
  241.  
  242.     if len(_cache) >= _MAXCACHE:
  243.         _cache.clear()
  244.     _cache[cachekey] = p
  245.     return p
  246.  
  247.  
  248. def _compile_repl(*key):
  249.     p = _cache_repl.get(key)
  250.     if p is not None:
  251.         return p
  252.     (repl, pattern) = None
  253.     
  254.     try:
  255.         p = sre_parse.parse_template(repl, pattern)
  256.     except error:
  257.         v = None
  258.         raise error, v
  259.  
  260.     if len(_cache_repl) >= _MAXCACHE:
  261.         _cache_repl.clear()
  262.     _cache_repl[key] = p
  263.     return p
  264.  
  265.  
  266. def _expand(pattern, match, template):
  267.     template = sre_parse.parse_template(template, pattern)
  268.     return sre_parse.expand_template(template, match)
  269.  
  270.  
  271. def _subx(pattern, template):
  272.     template = _compile_repl(template, pattern)
  273.     if not template[0] and len(template[1]) == 1:
  274.         return template[1][0]
  275.     
  276.     def filter(match, template = None):
  277.         return sre_parse.expand_template(template, match)
  278.  
  279.     return filter
  280.  
  281. import copy_reg
  282.  
  283. def _pickle(p):
  284.     return (_compile, (p.pattern, p.flags))
  285.  
  286. copy_reg.pickle(_pattern_type, _pickle, _compile)
  287.  
  288. class Scanner:
  289.     
  290.     def __init__(self, lexicon, flags = 0):
  291.         BRANCH = BRANCH
  292.         SUBPATTERN = SUBPATTERN
  293.         import sre_constants
  294.         self.lexicon = lexicon
  295.         p = []
  296.         s = sre_parse.Pattern()
  297.         s.flags = flags
  298.         for phrase, action in lexicon:
  299.             p.append(sre_parse.SubPattern(s, [
  300.                 (SUBPATTERN, (len(p) + 1, sre_parse.parse(phrase, flags)))]))
  301.         
  302.         s.groups = len(p) + 1
  303.         p = sre_parse.SubPattern(s, [
  304.             (BRANCH, (None, p))])
  305.         self.scanner = sre_compile.compile(p)
  306.  
  307.     
  308.     def scan(self, string):
  309.         result = []
  310.         append = result.append
  311.         match = self.scanner.scanner(string).match
  312.         i = 0
  313.         while None:
  314.             m = match()
  315.             if not m:
  316.                 break
  317.             j = m.end()
  318.             if i == j:
  319.                 break
  320.             action = self.lexicon[m.lastindex - 1][1]
  321.             if hasattr(action, '__call__'):
  322.                 self.match = m
  323.                 action = action(self, m.group())
  324.             if action is not None:
  325.                 append(action)
  326.             i = j
  327.             continue
  328.             return (result, string[i:])
  329.  
  330.  
  331.